package httpclient.encrypt;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.MessageDigest;

/**
 * Created by yangqy on 2017/2/15.
 */
public class AES {
    private static final IvParameterSpec DEFAULT_IV = new IvParameterSpec(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
    private Key key;
    private IvParameterSpec iv;
    private Cipher cipher;

    public AES(String key) {
        this(key, 128);
    }

    public AES(String key, int bit) {
        this(key, bit, (String)null);
    }

    public AES(String key, int bit, String iv) {
        if(bit == 256) {
            this.key = new SecretKeySpec(getHash("SHA-256", key), "AES");
        } else {
            this.key = new SecretKeySpec(getHash("MD5", key), "AES");
        }

        if(iv != null) {
            this.iv = new IvParameterSpec(getHash("MD5", iv));
        } else {
            this.iv = DEFAULT_IV;
        }

        this.init();
    }

    private static byte[] getHash(String algorithm, String text) {
        try {
            return getHash(algorithm, text.getBytes("UTF-8"));
        } catch (Exception var3) {
            throw new RuntimeException(var3.getMessage());
        }
    }

    private static byte[] getHash(String algorithm, byte[] data) {
        try {
            MessageDigest ex = MessageDigest.getInstance(algorithm);
            ex.update(data);
            return ex.digest();
        } catch (Exception var3) {
            throw new RuntimeException(var3.getMessage());
        }
    }

    private void init() {
        try {
            this.cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (Exception var2) {
            throw new RuntimeException(var2.getMessage());
        }
    }

    public String encrypt(String str) {
        try {
            return this.encrypt(str.getBytes("UTF-8"));
        } catch (Exception var3) {
            throw new RuntimeException(var3.getMessage());
        }
    }

    public String encrypt(byte[] data) {
        try {
            this.cipher.init(1, this.key, this.iv);
            byte[] ex = this.cipher.doFinal(data);
            return new String(Base64.getEncoder().encode(ex), "UTF-8");
        } catch (Exception var3) {
            throw new RuntimeException(var3.getMessage());
        }
    }

    public String decrypt(String str) {
        try {
            return this.decrypt(Base64.getDecoder().decode(str));
        } catch (Exception var3) {
            throw new RuntimeException(var3.getMessage());
        }
    }

    public String decrypt(byte[] data) {
        try {
            this.cipher.init(2, this.key, this.iv);
            byte[] ex = this.cipher.doFinal(data);
            return new String(ex, "UTF-8");
        } catch (Exception var3) {
            throw new RuntimeException(var3.getMessage());
        }
    }
}